import query from './atom/db/query';
import count from './atom/db/count';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	criteria: object;
	page_size: number;
	page_no: number;
}

function format(d: number) {
	const dt = new Date(d);
	return dt.getFullYear() + '-' + (((dt.getMonth() + 1) < 10) ? ('0' + (dt.getMonth() + 1)) : (dt.getMonth() + 1)) + '-' + ((dt.getDate() < 10) ? ('0' + (dt.getDate())) : (dt.getDate()));
}

async function atom(msg: Message) {
	const systemid = '5981';
	const q = query(action_id, session_id, spaceid, systemid);

	q.prepare('exam_room_date', ['_id', 'exam_date_id', 'exam_no', 'teaching_building', 'belong_area', 'floor_no', 'house_number', 'classroom_type', 'max_intake_capacity', 'exam_room_id', 'time_quantum_type', 'exam_room_state', 'fzno', 'exam_data'], msg.criteria, msg.page_size, msg.page_no, ['fzno']);

	const [t] = await q.exec();
	t.forEach((item: any) => {
		item['exam_data_str'] = format(item['exam_data']);
	});

	const c = count(action_id, session_id, spaceid, systemid);
	c.prepare('exam_room_date', msg.criteria);
	const [tc] = await c.exec();

	return { dataArray: t, count: tc };
}
